<?php


namespace app\home\model\page\circle;


use app\api\model\dao\Note;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;

class NoteListPage extends PageBase
{
    private $objNote;

    public function __construct()
    {
        parent::__construct();
        $this->objNote = new Note();
    }

    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            header('Location:/home/loginpage');
            return $this->getRes(Error::ERR_NO_AUTH);
        }
        try {
            $ret = $this->objNote->noteListPage();
        } catch (Exception $e) {
            return $this->getRes(Error::ERR_SYS, $e->getMessage());
        }
        $this->formatData($ret);

        $this->data = $ret;
        return $this->getRes();
    }

    public function formatData($objPage)
    {

        $objPage->each(function ($key, $value) {
            $type = '';
            if ($key->flag & Flag::FLAG_NOTE_EXQUISITE) {

                $type = $type . ',' . Flag::$note_type_map[Flag::FLAG_NOTE_EXQUISITE];
            }
            if ($key->flag & Flag::FLAG_NOTE_HOT) {
                $type = $type . ',' . Flag::$note_type_map[Flag::FLAG_NOTE_HOT];
            }
            if ($key->flag & Flag::FLAG_NOTE_ORIGIN) {
                $type = $type . ',' . Flag::$note_type_map[Flag::FLAG_NOTE_ORIGIN];
            }

            $key->flag = $type;
            $approve_status = [
                '0' => '未审核',
                '1' => '审核通过',
                '2' => '驳回',
            ];
            $key->approve_status = $approve_status[$key->approve_status];

        });
    }
}